<template>
  <div>
    <h3>用户登录信息管理</h3>

    <!-- 查询功能 -->
    <el-input v-model="searchQuery" placeholder="请输入服务名称进行查询" style="width: 300px; margin-bottom: 20px;" clearable></el-input>
    <el-button type="primary" @click="fetchMedicalServices">查询</el-button>

    <!-- 添加用户按钮 -->
    <!-- <el-button type="primary" @click="openDialog(null)">添加用户</el-button> -->

    <!-- 用户信息列表 -->
    <el-table :data="fetchMedicalServices" style="width: 100%">
      <el-table-column prop="username" label="用户名" width="180"></el-table-column>
      <el-table-column prop="role" label="角色" width="150"></el-table-column>
      <el-table-column prop="phone" label="电话号码" width="180"></el-table-column>

      <!-- 操作列 -->
      <el-table-column label="操作" width="180">
        <template v-slot="scope">
          <!-- <el-button size="small" type="primary" @click="openDialog(scope.row)">编辑</el-button> -->
          <el-button size="small" type="danger" @click="deleteUser(scope.row.username)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑用户信息对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible">
      <el-form :model="userForm" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="userForm.username" :disabled="isEditing"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input v-model="userForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="角色">
          <el-select v-model="userForm.role" placeholder="请选择角色">
            <el-option label="参保人员" value="InsuredPerson"></el-option>
            <el-option label="工作人员" value="Staff"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="电话号码">
          <el-input v-model="userForm.phone"></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveUser">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      searchQuery: '',
      userList: [],
      userForm: {
        username: '',
        password: '',
        role: '',
        phone: ''
      },
      dialogVisible: false,
      dialogTitle: '',
      isEditing: false
    };
  },
  computed: {
    fetchMedicalServices() {
      return this.userList.filter(institution =>
          institution.username.toLowerCase().includes(this.searchQuery.toLowerCase())
      );
    }
  },
  mounted() {
    this.fetchUserList();
  },
  methods: {
    // 获取用户列表
    fetchUserList() {
      axios.get('http://localhost:8081/user/users')
          .then(response => {
            this.userList = response.data
          })
          .catch(error => {
            console.error("Error fetching insured persons:", error);
          });
    },
    // 打开添加/编辑对话框
    openDialog(row) {

    },
    // 重置表单
    resetForm() {

    },
    // 保存用户信息

    },
    // 删除用户
    deleteUser(username) {

  }
};
</script>

<style scoped>
.el-button {
  margin-bottom: 20px;
}
.dialog-footer {
  text-align: right;
}
</style>
